/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controler.ControleClientes;
import dao.DaoClientes;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Tiago
 */
public class TelaHistoricoCliente extends javax.swing.JInternalFrame {
    DaoClientes daoCliente = new DaoClientes();
    
    
    private ResultSet rs;

    /**
     * Creates new form TelaHistoricoCliente
     */
    public TelaHistoricoCliente() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        comboBuscaCliente = new javax.swing.JComboBox();
        jTextFieldBuscaCliente = new javax.swing.JTextField();
        consultarCliente = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableCliente = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        nomeClienteHistorico = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        tableHistoricoCliente = new javax.swing.JTable();

        setClosable(true);
        setIconifiable(true);
        setTitle("Histórico Do Cliente");

        comboBuscaCliente.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        comboBuscaCliente.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Código", "Nome" }));
        comboBuscaCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboBuscaClienteActionPerformed(evt);
            }
        });

        jTextFieldBuscaCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldBuscaClienteActionPerformed(evt);
            }
        });

        consultarCliente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/lupa.png"))); // NOI18N
        consultarCliente.setToolTipText("Procurar Cliente");
        consultarCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                consultarClienteActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Vrinda", 1, 18)); // NOI18N
        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/1Cliente.png"))); // NOI18N
        jLabel3.setText("Clientes:");

        tableCliente.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(tableCliente);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/Histórico.png"))); // NOI18N
        jButton1.setText("Consultar Histórico");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(comboBuscaCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldBuscaCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(consultarCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel3))
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jTextFieldBuscaCliente, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(consultarCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboBuscaCliente))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1)
                .addContainerGap(189, Short.MAX_VALUE))
        );

        jLabel4.setFont(new java.awt.Font("Vrinda", 1, 18)); // NOI18N
        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/1Cliente.png"))); // NOI18N
        jLabel4.setText("Históricos do Cliente");

        jLabel1.setText("Nome:");

        nomeClienteHistorico.setEnabled(false);

        tableHistoricoCliente.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(tableHistoricoCliente);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nomeClienteHistorico, javax.swing.GroupLayout.PREFERRED_SIZE, 387, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(0, 20, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(nomeClienteHistorico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(175, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void comboBuscaClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboBuscaClienteActionPerformed

    }//GEN-LAST:event_comboBuscaClienteActionPerformed

    private void jTextFieldBuscaClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldBuscaClienteActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextFieldBuscaClienteActionPerformed

    private void consultarClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_consultarClienteActionPerformed
        try {
            this.prencherTabelaClientes();
        } catch (SQLException ex) {
            Logger.getLogger(TelaHistoricoCliente.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_consultarClienteActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        String idClienteHistorico =   tableCliente.getValueAt(tableCliente.getSelectedRow(), 0).toString();
        String nomeClienteHistorico =   tableCliente.getValueAt(tableCliente.getSelectedRow(), 1).toString();
        this.nomeClienteHistorico.setText(nomeClienteHistorico);
        daoCliente.consultaHistoricoCliente(Integer.parseInt(idClienteHistorico));
        try {
            preecnheTabHistoricoCliente(Integer.parseInt(idClienteHistorico));
        } catch (SQLException ex) {
            Logger.getLogger(TelaHistoricoCliente.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton1ActionPerformed
    public void preecnheTabHistoricoCliente(int idCliente) throws SQLException {
        
        ResultSet rs = daoCliente.consultaHistoricoCliente(idCliente);
        ResultSetMetaData rsmetadata = rs.getMetaData();
        
        //nº de colunas
        int columns = rsmetadata.getColumnCount();
        
        DefaultTableModel dtm = new DefaultTableModel();
        
        Vector colunsName = new Vector();
        Vector dataRows = new Vector();
        
        for (int i = 1; i < columns; i++) {
            colunsName.addElement(rsmetadata.getColumnName(i));
        }
        dtm.setColumnIdentifiers(colunsName);
        while(rs.next()){
            dataRows = new Vector();
            for(int j = 1 ; j < columns;j++){
                dataRows.addElement(rs.getString(j));
            }
            dtm.addRow(dataRows);
            
        } 
            this.tableHistoricoCliente.setModel(dtm);
            tableHistoricoCliente.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
           
          
        
        
    }
    
    
    
    
    public void prencherTabelaClientes() throws SQLException {

      


        ResultSet rs = daoCliente.retornaCliente(jTextFieldBuscaCliente.getText(), comboBuscaCliente.getSelectedIndex());
        ResultSetMetaData rsmetadata = rs.getMetaData();
        
        //nº de colunas
        int columns = rsmetadata.getColumnCount();
        
        DefaultTableModel dtm = new DefaultTableModel();
        
        Vector colunsName = new Vector();
        Vector dataRows = new Vector();
        
        for (int i = 1; i < columns; i++) {
            colunsName.addElement(rsmetadata.getColumnName(i));
        }
        dtm.setColumnIdentifiers(colunsName);
        while(rs.next()){
            dataRows = new Vector();
            for(int j = 1 ; j < columns;j++){
                dataRows.addElement(rs.getString(j));
            }
            dtm.addRow(dataRows);
            
        } 
            this.tableCliente.setModel(dtm);
            tableCliente.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
           
          
                
       
    }

    
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox comboBuscaCliente;
    private javax.swing.JButton consultarCliente;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextFieldBuscaCliente;
    private javax.swing.JTextField nomeClienteHistorico;
    private javax.swing.JTable tableCliente;
    private javax.swing.JTable tableHistoricoCliente;
    // End of variables declaration//GEN-END:variables
}
